package com.kejiguan.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.kejiguan.ui.adapter.NeItemChooseAdapter;
import com.kejiguan.ui.dialog.CommDialogUtil;
import com.kejiguan.ui.model.NeItem;
import com.kejiguan.ui.presenter.ChooseNeItemToExhibitionPresenter;
import com.kejiguan.ui.response.NeItemManagerResponse;
import com.kejiguan.ui.response.QueryAllNeItemResponse;
import com.kejiguan.ui.widget.MyLinearLayoutManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by YYY on 16/7/19.
 */
public class ChooseNeItemToExhibitionActivity extends Activity implements View.OnClickListener{

    private ChooseNeItemToExhibitionPresenter mPresenter;
    private RecyclerView neListView;
    private Button chooseBtn;
    private Button backBtn;


    private NeItemChooseAdapter adapter;

    private String exhibitionId;
    private String exhibitionName;

    private NeItem neItem;
    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        getIntentData();
        mPresenter=new ChooseNeItemToExhibitionPresenter();
        mPresenter.onCreate(ChooseNeItemToExhibitionActivity.this);
        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_choose_ne_layout);
        neListView= (RecyclerView) findViewById(R.id.ne_list);
        chooseBtn= (Button) findViewById(R.id.choose_btn);
        backBtn= (Button) findViewById(R.id.back_btn);

        MyLinearLayoutManager layoutManager=new MyLinearLayoutManager(ChooseNeItemToExhibitionActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        neListView.setLayoutManager(layoutManager);
        adapter=new NeItemChooseAdapter(ChooseNeItemToExhibitionActivity.this,null);
        neListView.setAdapter(adapter);

        chooseBtn.setOnClickListener(this);
        backBtn.setOnClickListener(this);
        mPresenter.queryNeItems();
    }


    public void getIntentData()
    {
        exhibitionId=getIntent().getStringExtra("exhibitionId");
        exhibitionName=getIntent().getStringExtra("exhibitionName");
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.back_btn:
                finish();
                break;
            case R.id.choose_btn:
                neItem=adapter.getCurrentNeItem();
                if (null==neItem)
                {
                    Toast.makeText(ChooseNeItemToExhibitionActivity.this,"请选择你要添加的网元",Toast.LENGTH_SHORT).show();
                }else
                {
                    CommDialogUtil.getInstance().show(ChooseNeItemToExhibitionActivity.this,"将网元"+neItem.getNeName()+"添加到展项"+exhibitionName+"中?",ChooseNeItemToExhibitionActivity.this,"添加","取消",0);

                }
                break;
            case R.id.dialog_confirm_btn:
                CommDialogUtil.getInstance().dismiss();
                chooseBtn.setClickable(false);
                mPresenter.attatchNeItem(neItem,exhibitionId);
                break;
            case R.id.dialog_cancel_btn:
                CommDialogUtil.getInstance().dismiss();
                break;
        }

    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(QueryAllNeItemResponse response) {

        if ("0000".equals(response.getRetCode()))
        {
            adapter.setNeItems(response.getEmptyNeItem());
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(NeItemManagerResponse response) {

        if ("0000".equals(response.getRetCode()))
        {
            finish();
        }else
        {
            chooseBtn.setClickable(true);
        }

    }
}
